myApp_.onPageInit('order_list', function (page) {
    //初始化分页数据
    ORDER_LIST.page = 0;
    ORDER_LIST.last_page = 1;

    //请求数据
    ORDER_LIST.fill_status();
    ORDER_LIST.request();
});


//订单状态数据
ORDER_STATUS_DATA = null;
if(!ORDER_STATUS_DATA){
	request_(
	    url_('api/V4/order_staus_list')
	    , {}
	    , function (d) {
	        if (d.code == 0) {

	            ORDER_STATUS_DATA = d.data;
	        }else {
	            my_tips_(d.msg);
	        }
	    }
	);
}



ORDER_LIST = {};
ORDER_LIST.page = 0;
ORDER_LIST.last_page = 1;
ORDER_LIST.status = '';
//翻页启动装置
ORDER_LIST.scroll_flag = true;

ORDER_LIST.data = {};

ORDER_LIST.__INIT__ = function (status) {
    ORDER_LIST.status = status;
    mainView.router.load({
        url: "src/my_center/order/order_list.html"
    });
}

/**
 * 翻页
 */
ORDER_LIST.scroll = function (e) {
    var me = $(e);
    var viewH = me.height();
    var contentH = me.get(0).scrollHeight;
    var scrollTop = me.scrollTop();
    if (scrollTop / (contentH - viewH) >= 0.95) {
        if (this.scroll_flag) {
            this.scroll_flag = false;
        } else {
            return;
        }
        this.request();
    }
}

/**
 * 请求数据
 */
ORDER_LIST.request = function () {
    if (ORDER_LIST.page >= ORDER_LIST.last_page) {
        return false;
    }
    //加载下一页
    ORDER_LIST.page++;
    request_(
        url_('api/V4/order_list')
        , {
            order_status_id: ORDER_LIST.status
            , page: ORDER_LIST.page
        }
        , function (d) {
            if (d.code == 0) {
                ORDER_LIST.page = d.data.current_page;
                ORDER_LIST.last_page = d.data.last_page;
                
                ORDER_LIST.data = d.data;
                ORDER_LIST.fill_list();
            } else {
                my_tips_(d.msg);
            }
        }
    );
}

/**
 * 状态筛选栏
 */
//传入状态code  返回状态
ORDER_LIST.order_status = function (e) {
	var title = '';
	ORDER_STATUS_DATA.forEach(function(v,i,arr){
		if(v.ordr_status_id == e){
			return title = v.title;
		}
	});
	return title;
};

ORDER_LIST.fill_status = function () {
	if(!ORDER_STATUS_DATA){
		request_(
	        url_('api/V4/order_staus_list')
	        , {}
	        , function (d) {
	            if (d.code == 0) {
	                for (var i in d.data) {
	                    if (d.data[i].ordr_status_id == ORDER_LIST.status) {
	                        d.data[i].active = 'active';
	                    }else{
	                        d.data[i].active = '';
	                    }
	                }
	                ORDER_STATUS_DATA = d.data;
	                
	                tpl_fill(
	                    ORDER_LIST.status_tpl
	                    , d.data
	                    , 'div#select_list'
	                );
	            } else {
	                my_tips_(d.msg);
	            }
	        }
	    );
	}else{
		for (var i in ORDER_STATUS_DATA) {
            if (ORDER_STATUS_DATA[i].ordr_status_id == ORDER_LIST.status) {
                ORDER_STATUS_DATA[i].active = 'active';
            }else{
                ORDER_STATUS_DATA[i].active = '';
            }
        }
        
        tpl_fill(
            ORDER_LIST.status_tpl
            , ORDER_STATUS_DATA
            , 'div#select_list'
        );
		
	}
    
}

ORDER_LIST.get_status_list = function (t) {
    $(t).addClass('active').siblings('.active').removeClass('active');

    var status = $(t).data('status');
    ORDER_LIST.status = status;
    //清空当前内容 请求新内容
    $('#order_container').html('');
    ORDER_LIST.page = 0;
    ORDER_LIST.last_page = 1;
    ORDER_LIST.request();
}

ORDER_LIST.status_tpl = '' +
    '{{#each this}}' +
    '<a style="display: inline-block;width: 20%;float: none;height: 2.8rem;" onclick="ORDER_LIST.get_status_list(this)" data-status="{{ordr_status_id}}" class="tab-link mypay {{active}}">{{title}}</a>' +
    '{{/each}}';

/**
 * 订单列表
 */
ORDER_LIST.fill_list = function () {
    $.tpl({
        url: 'src/my_center/order/order_list_tpl.html'
        , context: ORDER_LIST.data.data
        , elem: '#order_container'
        , append: true
        , before: function (param) {
            if (ORDER_LIST.page >= ORDER_LIST.last_page) {
                param.html += '<p class="daodile_" style="text-align: center;">没有更多订单</p>'
            }
            return param;
        }
        , after: function () {
            //scroll_flag 封印解除
            ORDER_LIST.scroll_flag = true;
        }
    });
}

//ORDER_LIST.detail = function(){
//	var order_id = 1234565;
//	go_('order_detail/'+order_id);
//}
//
////确认收货操作
//ORDER_LIST.confirm_receipt = function(oid){
//    use("tool/yue/yue");
//    YUE.__INIT__(function(code,pwd){
//    	request_(
//    		url_('api/V4/order_querenshouhuo')
//    		,{
//    			order_id: oid
//    			,pwd: pwd
//    		}
//    		,function(d){
//    			if (d.code == 0) {
//    			 	my_tips_('确认收货成功',function(){
//    			 		//刷新当前状态订单列表
//					    $('#order_container').html('');
//					    ORDER_LIST.page = 0;
//					    ORDER_LIST.last_page = 1;
//					    ORDER_LIST.request();
//    			 	});
//	            } else {
//	                my_tips_(d.msg);
//	            }
//    		}
//    	);
//    });
//}
//


//取消订单操作
ORDER_LIST.cancel_order = function(oid){
    request_(
        url_('addons/execute/user-App-cancel_order')
        ,{
            order_id: oid
        }
        ,function(d){
            if (d.code == 0) {
                my_tips_('取消订单成功',function(){
                    //刷新当前状态订单列表
                    $('#order_container').html('');
                    ORDER_LIST.page = 0;
                    ORDER_LIST.last_page = 1;
                    ORDER_LIST.request();
                });
            } else {
                my_tips_(d.msg);
            }
        }
    );
}


